package com.jufanshare.myblog.controller.admin.base;

import com.jufanshare.myblog.common.util.IPUtil;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class BaseController {
    /**
     * 得到request对象
     */
    public HttpServletRequest getRequest() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        return request;
    }
    public HttpServletResponse getResponse() {
        HttpServletResponse request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
        return request;
    }
    public HttpSession getSession(){
        return getRequest().getSession();
    }
    /**
     * 获取用户IP
     * @return
     */
    public String getIp(){
        return IPUtil.getIpAddr(getRequest());
    }
}
